package ZooManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* User: James Barker
* Date: 16/08/12
*/
public class CageAddUI extends JDialog implements ActionListener {
    Boolean cancelled = true;
    Cage newCage;
    JTextField enclosureNameTextField;
    JSpinner enclosureNumberSpinner;
    JSpinner enclosureSizeSpinner;
    JTextField enclosureTypeTextField;
    JSpinner noOfEntriesSpinner;
    JTextField exhibitNameTextField;
    JTextField exhibitDescTextField;


    /**
    * Add Cage UI Constructor
    */
    public CageAddUI() {
        createCageDetailPanel();

        this.setTitle("New Cage");
        this.setSize(300, 280);
        this.setLocationRelativeTo(this);
        this.setModal(true);
        this.setVisible(true);
        this.pack();
    }


    public void createCageDetailPanel() {
        JPanel cageDetailPanel = new JPanel(new GridLayout(8, 2, 5, 5));
        cageDetailPanel.setBorder(BorderFactory.createTitledBorder("Add Cage"));
        cageDetailPanel.add(new JLabel("Enclosure Name:"));
        enclosureNameTextField = new JTextField();
        cageDetailPanel.add(enclosureNameTextField);
        cageDetailPanel.add(new JLabel("Enclosure Number:"));
        SpinnerNumberModel enclosureNumberModel = new SpinnerNumberModel(1, 1, 100, 1);
        enclosureNumberSpinner = new JSpinner(enclosureNumberModel);
        cageDetailPanel.add(enclosureNumberSpinner);
        cageDetailPanel.add(new JLabel("Enclosure Size:"));
        SpinnerNumberModel enclosureSizeModel = new SpinnerNumberModel(1, 1, 50, 1);
        enclosureSizeSpinner = new JSpinner(enclosureSizeModel);
        cageDetailPanel.add(enclosureSizeSpinner);
        cageDetailPanel.add(new JLabel("Enclosure Type:"));
        enclosureTypeTextField = new JTextField();
        cageDetailPanel.add(enclosureTypeTextField);
        cageDetailPanel.add(new JLabel("Number of Entries:"));
        SpinnerNumberModel noOfEntriesModel = new SpinnerNumberModel(1, 1, 3, 1);
        noOfEntriesSpinner = new JSpinner(noOfEntriesModel);
        cageDetailPanel.add(noOfEntriesSpinner);
        cageDetailPanel.add(new JLabel("Exhibit Name:"));
        exhibitNameTextField = new JTextField();
        cageDetailPanel.add(exhibitNameTextField);
        cageDetailPanel.add(new JLabel("Exhibit Description:"));
        exhibitDescTextField = new JTextField();
        cageDetailPanel.add(exhibitDescTextField);
        cageDetailPanel.add(new JLabel(""));
        JButton saveCageButton = new JButton("Save Cage");
        saveCageButton.addActionListener(this);
        cageDetailPanel.add(saveCageButton);
        this.add(cageDetailPanel);
    }


    public Boolean isCancelled() {
        return cancelled;
    }



    public Cage getNewCage() {
        return newCage;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        newCage = new Cage(enclosureNameTextField.getText(), Integer.parseInt(enclosureNumberSpinner.getValue().toString()), Integer.parseInt(enclosureSizeSpinner.getValue().toString()), enclosureTypeTextField.getText(), Integer.parseInt(noOfEntriesSpinner.getValue().toString()), exhibitNameTextField.getText(), exhibitDescTextField.getText());
        cancelled = false;
        for (int i = 0; i < newCage.getNoOfEntries(); i++) {
            EntryAddUI entryAddUI = new EntryAddUI();
            newCage.addEntry(entryAddUI.getNewEntry());
            entryAddUI.dispose();
        }
        this.setVisible(false);
    }
}
